package com.lebang.controller.api.earned;

import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.Sessionid;
import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.component.RedisClient;
import com.lebang.domain.MemberEarnLookRecord;
import com.lebang.domain.PointAdvertisementSetting;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.NewsEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.LuckDrawService;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.NewsDetailService;
import com.lebang.service.PointService;
import com.lebang.service.earn.AppEarnLookService;
import com.lebang.service.sys.DictService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hwq
 * @Date: 2020/3/10 9:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/app/news")
public class NewsEarnApi {
    @Autowired
    private AppEarnLookService appEarnLookService;
    @Autowired
    private PointService pointService;
    private static final String XWZ = "xwz";
    private static final String KEY = "lock:newsearn:";
    @Autowired
    private LuckDrawService luckDrawService;

    @Autowired
    private MemberNewTaskService newTaskService;
    @Autowired
    private NewsEarnedFeign newsEarnedFeign;

    @Autowired
    private DictService dictService;


    /**
     * 开关
     *
     * @param request
     * @return
     */
//    @Require
    @Sessionid
    @PostMapping("/enable")
    public JsonData enable(HttpServletRequest request, String key) {


            RequestUserInfo userInfo = new RequestUserInfo(request);
            return newsEarnedFeign.enable(userInfo, key);


    }

    /**
     * 任务列表
     *
     * @param request
     * @return
     */
//    @Require
    @Sessionid
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request, String key, Long page, Integer limit) {


            RequestUserInfo userInfo = new RequestUserInfo(request);
            return newsEarnedFeign.list(userInfo, key, page, limit);



    }

    /**
     * 领取任务
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/pull")
    @MemberLock
    public JsonData pull(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        try {
            JsonData jsonData = newsEarnedFeign.pull(userInfo);
            if (jsonData.isSuccess()) {
                //成功
                JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()));
                PointAdvertisementSetting setting = luckDrawService.selectAdByBrand(userInfo.getAppStore(), Integer.parseInt(userInfo.getVersion()));
                setting.setType(Integer.parseInt(setting.getKey()));
                object.put("ad", setting);
                jsonData.setData(object);
            } else {
                return jsonData;
            }

            return jsonData;
        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("205", "服务器异常",e.getMessage());
        }

    }

    /**
     * 提交任务
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/push")
    @ValidCode
    @MemberLock
    public JsonData push(HttpServletRequest request, Long id) {


        RequestHeaders header = new RequestHeaders(request);
        RequestUserInfo userInfo = new RequestUserInfo(request);
        Result<Integer> result = newsEarnedFeign.push(userInfo, id);
        if (result.isSuccess()) {
            //成功
            pointService.addPoint(header.getUserid(), result.getData(), TaskCategory.NEWS_EARN, header, "");
            newTaskService.pushRecord(TaskCategory.NEWS_EARN, header.getUserid());
        } else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
        return Callback.success(result.getStatus(), result.getMessage(), result.getData());


    }

    @Require
    @Sessionid
    @PostMapping("/double")
    @ValidCode
    @MemberLock
    public JsonData doubleCount(HttpServletRequest request, Long id) {

        if (dictService.selectByKeyName("open_new_news").getKeyvalue().equals("1")) {
            RequestHeaders header = new RequestHeaders(request);
            RequestUserInfo userInfo = new RequestUserInfo(request);
            Result<Integer> result = newsEarnedFeign.doubleCount(userInfo, id);
            if (result.isSuccess()) {
                //成功
                pointService.addPoint(header.getUserid(), result.getData(), TaskCategory.NEWS_EARN, header, "奖励翻倍");
            } else {
                return Callback.fail(result.getStatus(), result.getMessage());
            }
            return Callback.success(result.getStatus(), result.getMessage(), result.getData());
        } else {
            RequestHeaders header = new RequestHeaders(request);
            try {
                MemberEarnLookRecord record = appEarnLookService.selectRecordById(id);
                if (record == null) {
                    return Callback.fail("201", "任务不存在..");
                }

                if (!record.getMemberId().equals(header.getUserid())) {
                    return Callback.fail("204", "任务不存在.....");
                }
                if (!record.getStatus().equals(1)) {
                    return Callback.fail("205", "任务未成功......");
                }
                if (!record.getIsDouble().equals(1)) {
                    return Callback.fail("206", "任务不能翻倍......");
                }
                if (record.getIsDoubleStatus().equals(1)) {
                    return Callback.fail("207", "任务已翻倍......");
                }
                if (record.getTradeTime() == null) {
                    return Callback.fail("208", "任务未完成......");
                }
                int count = record.getPoint();
                appEarnLookService.doubleCount(header, record);

                pointService.addPoint(header.getUserid(), count, TaskCategory.NEWS_EARN, header, "奖励翻倍");
                return Callback.success("success", "翻倍成功", count);
            } catch (Exception e) {
                e.printStackTrace();
                return Callback.fail("200", "服务器异常", e);
            }
        }

    }


    /**
     * 新闻分类列表
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/category")
    public JsonData category(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.category(userInfo);

    }


    /**
     * 新闻列表
     *
     * @param request
     * @return
     */
//    @Require
    @Sessionid
    @PostMapping("/news")
    public JsonData news(HttpServletRequest request, String category, Long page, Integer limit) {

        return newsEarnedFeign.news(category, page, limit);

    }

    /**
     * 获取用户新闻种类
     */
    @Sessionid
    @PostMapping("/membercategory")
    public JsonData getNewsCategory(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.getNewsCategory(userInfo);

    }

    /**
     * 更新用户种类
     * @param request
     * @param categorys
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/updatecategory")
    public JsonData updateCategory(HttpServletRequest request,String categorys){
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.updateCategory(userInfo,categorys);
    }

    /**
     * 获取用户资讯收藏记录
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/record")
    public JsonData getRecord(HttpServletRequest request,Long page,Integer limit) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.getRecord(userInfo,page,limit);
    }

    /**
     * 用户资讯收藏
     * @param request
     * @param title
     * @param source
     * @param itemId
     * @return
     */
    @Require
    @MemberLock
    @Sessionid
    @PostMapping("/collect")
    public JsonData collect(HttpServletRequest request, String title, String source, String itemId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.collect(userInfo, title, source, itemId);
    }

    /**
     * 用户取消资讯收藏
     * @param request
     * @param itemId
     * @return
     */
    @Require
    @Sessionid
    @MemberLock
    @PostMapping("/del/collect")
    public JsonData delCollect(HttpServletRequest request, String itemId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.delCollect(userInfo,itemId);
    }

    /**
     * 资讯是否被收藏
     * @param request
     * @param itemId
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/isCollect")
    public JsonData isCollect(HttpServletRequest request, String itemId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.isCollect(userInfo, itemId);
    }

    /**
     * 资讯查询
     * @param request
     * @param itemId
     * @return
     */
    @Sessionid
    @PostMapping("/get")
    public JsonData get(HttpServletRequest request, String itemId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return newsEarnedFeign.get(userInfo, itemId);
    }
}
